package com.meixiang.view.filtermenu;

import android.annotation.SuppressLint;
import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.meixiang.R;

import java.util.ArrayList;
import java.util.List;

/**
 * 筛选菜单栏
 *
 * @author LZRUI
 */
public class FilterMenuBar extends LinearLayout {

    private List<String> mMenuTextList;
    private List<FilterMenuButton> mButtonList;

    private Context mContext;
    private FilterMenuBarItemCheckListener filterMenuBarItemCheckListener;

    public void setFilterMenuBarItemCheckListener(FilterMenuBarItemCheckListener filterMenuBarItemCheckListener) {
        this.filterMenuBarItemCheckListener = filterMenuBarItemCheckListener;
    }

    public FilterMenuBar(Context context) {
        this(context, null);
    }

    public FilterMenuBar(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    @SuppressLint("NewApi")
    public FilterMenuBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
        init();
    }

    private void init() {
        setOrientation(HORIZONTAL);
        setGravity(Gravity.CENTER_VERTICAL);
    }

    /**
     * 创建menu按钮
     *
     * @param text 显示的文本
     * @return
     */
    private FilterMenuButton createItemView(String text) {
        FilterMenuButton itemView = new FilterMenuButton(mContext);
        LayoutParams param = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, 1.0f);
        itemView.setLayoutParams(param);
        itemView.setGravity(Gravity.CENTER);
        itemView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 13);
        itemView.setTextColor(ContextCompat.getColor(mContext, R.color.filter_menu_text_color));
        itemView.setText(text);
        return itemView;
    }

    /**
     * 创建分割线
     *
     * @return
     */
    private ImageView createLine() {
        ImageView view = new ImageView(mContext);
        view.setBackgroundResource(R.mipmap.shop_fengexian);
        LayoutParams param = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
        view.setLayoutParams(param);
        return view;
    }

    private void createBarView() {
        int len = mMenuTextList.size();
        if (len > 0) {
            if (mButtonList == null) mButtonList = new ArrayList<>();
            for (int i = 0; i < len; i++) {
                final FilterMenuButton itemView = createItemView(mMenuTextList.get(i));
                itemView.setTag(i);
                // 添加FilterMenuButton的点击事件
                itemView.setFilterMenuButtonClickListener(new FilterMenuButton.FilterMenuButtonClickListener() {
                    @Override
                    public void onCheckedListener(boolean isOpen) {
                        if (filterMenuBarItemCheckListener == null) {
                            throw new RuntimeException("FilterMenuBar 必须实现 FilterMenuBarItemCheckListener 接口,才能实现点击事件");
                        }
                        int position = (Integer) itemView.getTag();
                        filterMenuBarItemCheckListener.onFilterMenuBarItemCheck(position, isOpen);
                        if (isOpen) {
                            // 处于打开状态的话，就必须关闭其他button
                            refreshButtonStatus(position);
                        }
                    }
                });
                mButtonList.add(itemView);
                this.addView(itemView);
                if (i != len - 1) {
                    // 每个item之间添加分割线
                    this.addView(createLine());
                }
            }
        }
    }

    /**
     * 当其中一个button的状态变成true的时候，其余的button状态必须为false
     */
    private void refreshButtonStatus(int position) {
        if (!mButtonList.isEmpty()) {
            int len = mButtonList.size();
            for (int i = 0; i < len; i++) {
                if (position != i) mButtonList.get(i).setChecked(false);
            }
        }
    }

    /**
     * 添加item
     *
     * @param items
     * @return
     */
    public void setItems(List<String> items) {
        if (mMenuTextList == null) mMenuTextList = new ArrayList<>();
        if (!items.isEmpty()) {
            mMenuTextList.clear();
            mMenuTextList.addAll(items);
        }
        createBarView();
    }

    /**
     * 筛选栏item选择监听
     */
    public interface FilterMenuBarItemCheckListener {
        void onFilterMenuBarItemCheck(int position, boolean isOpen);
    }

}
